package com.cskaoyan.service;

import com.cskaoyan.bean.PO.MarketKeyword;
import com.cskaoyan.bean.PO.MarketKeywordExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.experimental.Helper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminKeywordsServiceImpl
 * @Description TODO
 * @Author ke
 * @Date 2022/8/13 18:15
 * @Version 1.0
 */
@Service
public class AdminKeywordsServiceImpl implements AdminKeywordsService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;


    @Override
    public CommonData<MarketKeyword> list(BasePageInfo info, String keyword, String url) {
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        if (keyword != null) {
            criteria.andKeywordLike("%"+keyword+"%");
        }
        if (url != null) {
            criteria.andUrlLike("%"+url+"%");
        }

        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(info.getPage(),info.getLimit());

        example.setOrderByClause(info.getSort()+" "+info.getOrder());

        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo(marketKeywords);


        return CommonData.data(pageInfo);
    }

    @Override
    public MarketKeyword create(MarketKeyword bo) {

       bo.setAddTime(new Date());

       bo.setUpdateTime(new Date());

        marketKeywordMapper.insertSelective(bo);

        return bo;
    }

    @Override
    public MarketKeyword update(MarketKeyword bo) {

        marketKeywordMapper.updateByPrimaryKeySelective(bo);


        return bo;
    }

    @Override
    public MarketKeyword delete(MarketKeyword bo) {

        bo.setDeleted(true);
        marketKeywordMapper.updateByPrimaryKeySelective(bo);


        return bo;
    }
}
